package com.huanglj.himalaya.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;

import com.bumptech.glide.Glide;
import com.huanglj.himalaya.R;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class PlayerTrackPagerAdapter extends PagerAdapter {

    private List<Track> mTrackList = new ArrayList<>();
    @Override
    public int getCount() {
        return mTrackList.size();
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View inflate = LayoutInflater.from(container.getContext()).inflate(R.layout.item_track, container, false);
        container.addView(inflate);
        //设置数据找到控件
        CircleImageView item = inflate.findViewById(R.id.track_pager_item);
        Track track = mTrackList.get(position);
        Glide.with(container.getContext()).load(track.getCoverUrlLarge()).into(item);
        return inflate;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }


    public void setData(List<Track> trackList) {
        mTrackList.clear();
        mTrackList.addAll(trackList);
        notifyDataSetChanged();
    }
}
